package com.ycz.service;

import com.ycz.entity.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author ycz
 * @date 2021/6/8
 * @description TODO
 *
 */
@Component
public class UserService implements InitializingBean, BeanFactoryPostProcessor/*, BeanDefinitionRegistryPostProcessor*/ {

    private User user;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
        if (Objects.isNull(user)) {
            System.out.println("user is null");
            user = new User();
            user.setDesc("init by afterPropertiesSet");
        } else {
            System.out.println("user is not null,user==" + user.toString());
        }
    }

    public void print() {
        System.out.println("this is print" + user.toString());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("postProcessBeanFactory===");
        if (Objects.isNull(user)) {
            System.out.println("user is null");
            user = new User();
            user.setDesc("init by postProcessBeanFactory");
        } else {
            System.out.println("user is not null,user==" + user.toString());
        }
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
